package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.entity.Elder;
import com.ruoyi.system.domain.entity.Room;
import com.ruoyi.system.mapper.ElderMapper;
import com.ruoyi.system.mapper.RoomMapper;
import com.ruoyi.system.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
    @Autowired
    private ElderMapper elderMapper;

    @Override
    public Room getWithElders(Long id) {
        Room room = this.getById(id);
        if (room != null) {
            List<Elder> elders = elderMapper.selectByRoomId(id);
            room.setElders(elders);
        }
        return room;
    }

    @Override
    public Room addToRoom(Long id, Elder elder) {
        Room room = this.getById(id);
        if (room != null) {
            int total = room.getCapacity();
            List<Elder> elders = elderMapper.selectByRoomId(id);
            int occupy = elders.size();
            if (total - occupy > 0) {
                elders.add(elder);
                room.setElders(elders);
            }
        }
        return room;
    }

}
